<template>
  <div class="table-box">
    <div class="table-search">
      <el-form :inline="true" :model="form">
        <el-form-item>
          <el-input v-model="form.merchant_id" placeholder="商户ID" />
        </el-form-item>
        <el-form-item>
          <el-input v-model="form.order_no" placeholder="订单流水号" />
        </el-form-item>
        <el-form-item>
          <el-select v-model="form.status" placeholder="订单状态" style="width:120px;">
            <el-option
              v-for="item in statusList"
              :key="item.type"
              :label="item.name"
              :value="item.type"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-date-picker
            v-model="form.timeData"
            type="datetimerange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="截止日期" :default-time="pickerDefaultTime"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onQuery">查询</el-button>
          <el-button @click="onReset">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="card content-box">
      <div class="table">
        <el-table :data="tableData" border v-loading="loading">
          <el-table-column prop="id" label="ID" align="center" />
          <el-table-column prop="entrust_id" label="挂单ID" align="center" />
          <el-table-column prop="order_no" label="订单流水号" align="center" />
          <el-table-column prop="from_merchant_id" label="发起商户ID" align="center" />
          <el-table-column prop="to_merchant_id" label="接收商户ID" align="center"/>
          <el-table-column prop="from_coin_name" label="原始币种" align="center"/>
          <el-table-column prop="to_coin_name" label="目标币种" align="center" />
          <el-table-column prop="price" label="价格" align="center" />
          <el-table-column prop="from_quantity" label="数量" align="center" />
          <el-table-column prop="to_quantity" label="兑换数量" align="center" />
          <el-table-column prop="platform_fee" label="平台手续费" align="center" />
          <el-table-column prop="platform_fee_ratio" label="平台手续费率" align="center" />
          <el-table-column prop="entrust_fee" label="商户手续费" align="center" />
          <el-table-column prop="entrust_fee_ratio" label="商户手续费率" align="center" />
          <el-table-column prop="status" label="状态" align="center" :formatter="formatterStatus" />
          <el-table-column prop="createtime" label="创建时间" align="center" :formatter="formatterTime" width="180"/>
          <el-table-column prop="expire_time" label="过期时间" align="center" :formatter="formatterTime" width="180"/>
        </el-table>
        <div class="table-pagination">
          <el-pagination
            :current-page="pageable.pageNum"
            :page-size="pageable.pageSize"
            :page-sizes="[10, 25, 50, 100]"
            :background="true"
            layout="total, sizes, prev, pager, next, jumper"
            :total="pageable.total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          ></el-pagination>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted, reactive } from "vue";
import { formatterTime, timestamp, pickerDefaultTime } from "@/utils/utils";
import { swapOrderApi } from "@/api/payServerse/swap";

const loading = ref(false);
const tableData = ref([]);

const formData = {
  merchant_id: "",
  order_no: "",
  status: "",
  timeData: [],
};
const form = reactive({ ...formData });
const statusList = [
  { type: "0", name: "提交" },
  { type: "1", name: "完成" },
  { type: "2", name: "失败" },
];
const pageable = reactive({
  pageNum: 1,
  pageSize: 10,
  total: 0,
});

// 参数
const param = () => {
  return {
    page: pageable.pageNum,
    pagesize: pageable.pageSize,
    merchant_id: form.merchant_id,
    order_no: form.order_no,
    status: form.status,
    start: form.timeData[0] ? timestamp(form.timeData[0].valueOf()) : "",
    end: form.timeData[1] ? timestamp(form.timeData[1].valueOf()) : "",
  };
};

//格式化
const formatterStatus = (row, column, cellValue, index) => {
  let name = "";
  statusList.filter((item) => {
    if (item.type == cellValue) {
      name = item.name;
    }
  });
  return name;
};

// 获取表格数据
const getTbaleData = async () => {
  loading.value = true;

  try {
    const res = await swapOrderApi(param());
    loading.value = false;
    tableData.value = res.data.results;
    pageable.total = res.data.count;
  } catch (error) {
    loading.value = false;
  }
};
// 重置
const onReset = () => {
  Object.assign(form, formData);
};
// 查询
const onQuery = async () => {
  getTbaleData();
};
// 条数
const handleSizeChange = (number) => {
  pageable.pageSize = number;
  getTbaleData();
};
// 页码
const handleCurrentChange = (number) => {
  pageable.pageNum = number;
  getTbaleData();
};
onMounted(() => {
  getTbaleData();
});
</script>
